﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace DrawControl.UI.Common.DrawControCenter.DrawSeal
{
    public class RectangleSeal : Control, IDrawControl
    {
        public RectangleSeal():this(160,160)
        {
        }
        public RectangleSeal(int width,int height)
        {
            SetStyle(
         ControlStyles.ResizeRedraw
         | ControlStyles.DoubleBuffer
         | ControlStyles.UserPaint
         | ControlStyles.AllPaintingInWmPaint
         | ControlStyles.SupportsTransparentBackColor,
         true
         );
            this.ForeColor = Color.Red;
            this.BackColor = Color.Transparent;
            this.BorderColor = Color.Red;
            this.Width = width;
            this.Height = height;
            this.Font = new Font("SimSun", 32, FontStyle.Bold);
            this.Text = "测试";
            Draw();
        }
        public Color BorderColor { get; set; }
        public float BorderSize { get; set; } = 1f;
        public Image Image { get; private set; }
        public Form FormType { get; set; }
        public byte RectangleSealType { get; set; } = 0;
        public void Save(string fileName)
        {
            Image?.Save(fileName);
        }

        public void Save(Stream stream, ImageFormat format)
        {
            Image?.Save(stream, format);
        }
        public void DrawName(string name)
        {
            this.Text = name;
            RectangleSealType = 1;
            Draw(RectangleSealType);
        }
        public void DrawNameWithBorder(string name)
        {
            this.Text = name;
            RectangleSealType = 2;
            Draw(RectangleSealType);
        }
        public void DrawSquare(string name)
        {
            this.Text = name;
            RectangleSealType = 3;
            Draw(RectangleSealType);
        }
        private void Draw(byte type)
        {
            if (string.IsNullOrWhiteSpace(this.Text)) return;

            using (Graphics g = Graphics.FromImage(Image))
            {
                g.Smooth();
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                g.Clear(this.BackColor);
                if (type == 1)
                    Draw1(g);
                else if (type == 2)
                    Draw2(g);
                else if (type == 3)
                    Draw3(g);
            }
            this.Invalidate();
        }
        private void Draw1(Graphics g)
        {
            var nameSize = g.MeasureString(this.Text, this.Font);
            PointF point = PointF.Empty;
            var left = (this.Width - nameSize.Width) / 2f;
            var top = (this.Height - nameSize.Height) / 2f;
            point = new PointF(left, top);
            using var sb = new SolidBrush(this.ForeColor);
            g.DrawString(this.Text, this.Font, sb, point);
        }
        private void Draw2(Graphics g)
        {
            var nameSize = g.MeasureString(this.Text, this.Font);
            PointF point = PointF.Empty;
            var left = (this.Width - nameSize.Width) / 2f;
            var top = (this.Height - nameSize.Height) / 2f;
            point = new PointF(left, top);
            using var sb = new SolidBrush(this.ForeColor);
            g.DrawString(this.Text, this.Font, sb, point);
            using Pen pen = new Pen(this.ForeColor, BorderSize);
            g.DrawRectangle(pen, new RectangleF(BorderSize / 2f - 0.5f, BorderSize / 2f - 0.5f, this.Width - BorderSize, this.Height - BorderSize));
        }
        private void Draw3(Graphics g)
        {
            using var pen = new Pen(this.BorderColor, this.BorderSize);
            using var sb = new SolidBrush(this.ForeColor);
            RectangleF rect = new RectangleF(BorderSize / 2f-0.5f, BorderSize / 2f - 0.5f, this.Width - this.BorderSize, this.Height - this.BorderSize);
            g.DrawRectangle(pen, rect);

            var textSize = g.MeasureString("之印", this.Font);
            var left = (this.Width / 2 - textSize.Width /2f + this.Padding.Left) / 2;
            var perHeght = (this.Height - this.BorderSize * 4 - textSize.Height * 2) / 3;

            PointF point1 = new PointF(left + this.BorderSize * 2, perHeght + this.BorderSize * 2);
            g.DrawString("之", this.Font, pen.Brush, point1);
            PointF point2 = new PointF(left + this.BorderSize * 2, perHeght * 2 + this.BorderSize * 2 + textSize.Height);
            g.DrawString("印", this.Font, pen.Brush, point2);

            var nameSize = g.MeasureString(this.Text, this.Font);
            int length = this.Text.Length;
            var left2 = (this.Width / 2 - this.BorderSize - nameSize.Width / length) / 2;
            var height = (this.Height - this.BorderSize * 4 - nameSize.Height * length) / (length + 1);
            for (var i = 0; i < length; i++)
            {
                PointF point = new PointF(this.Width / 2+(this.Width/2 - textSize.Width)/2+ this.Padding.Left+ this.Padding.Right, height * (i + 1) + this.BorderSize * 2 + nameSize.Height * i);
                g.DrawString(this.Text[i].ToString(), this.Font, sb, point);
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (Image != null && RectangleSealType != 0)
            {
                e.Graphics.DrawImage(Image, 0, 0, Image.Width, Image.Height);
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);

            if (RectangleSealType != 0)
            {
                Image?.Dispose();
                Image = new Bitmap(Width == 0 ? 1 : Width, Height == 0 ? 1 : Height);
                Draw(RectangleSealType);
            }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            Image?.Dispose();
        }

        public void Draw()
        {
            Image?.Dispose();
            Image = new Bitmap(Width == 0 ? 1 : Width, Height == 0 ? 1 : Height);
            Draw(RectangleSealType);
        }
    }
}
